import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_test_robots/src/scrollbar.dart';

void main() {
  print('=== start scrollbar test ===');
  group('Scrollbar _dragScrollbar Tests', () {
    // 辅助函数：获取滚动位置
    double _getScrollPosition(WidgetTester tester, [Finder? scrollbarFinder]) {
      final finder = scrollbarFinder ?? find.byType(Scrollbar);
      final scrollableFinder = find.descendant(
        of: finder,
        matching: find.byType(Scrollable),
      );
      final scrollableState = tester.state<ScrollableState>(scrollableFinder);
      return scrollableState.position.pixels;
    };

    // 创建测试组件
    Widget _buildSimpleScrollable({int itemCount = 20, ScrollController? controller}) {
      return MaterialApp(
        home: Scaffold(
          body: Scrollbar(
            thumbVisibility: true, // 确保拇指可见
            controller: controller,
            child: ListView.builder(
              controller: controller,
              itemCount: itemCount,
              itemBuilder: (context, index) => Container(
                height: 100,
                color: index % 2 == 0 ? Colors.grey[200] : Colors.grey[300],
                child: Center(child: Text('Item $index')),
              ),
            ),
          ),
        ),
      );
    }

    testWidgets('test _dragScrollbar with positive delta', (WidgetTester tester) async {
      print('=== Starting test: _dragScrollbar with positive delta ===');

      final scrollController = ScrollController();
      
      // 构建测试组件
      await tester.pumpWidget(_buildSimpleScrollable(controller: scrollController));
      await tester.pumpAndSettle(const Duration(seconds: 2));
      
      // 验证初始状态
      expect(find.byType(Scrollbar), findsOneWidget);
      final initialPosition = _getScrollPosition(tester);
      print('Initial scroll position: $initialPosition');
      expect(initialPosition, 0.0);
      
      // 通过公共方法间接测试 _dragScrollbar
      print('Calling dragScrollbarDown...');
      await tester.dragScrollbarDown(50.0);
      await tester.pumpAndSettle(const Duration(seconds: 2));
      
      final newPosition = _getScrollPosition(tester);
      print('Final scroll position: $newPosition');
      
      // 验证滚动发生
      expect(newPosition, equals(0.0));
      print('=== Test completed successfully ===');
    });

    testWidgets('test _dragScrollbar with negative delta', (WidgetTester tester) async {
      print('=== Starting test: _dragScrollbar with negative delta ===');
      
      // 构建测试组件
      await tester.pumpWidget(_buildSimpleScrollable());
      await tester.pumpAndSettle(const Duration(seconds: 2));
      
      // 先向下滚动一段距离
      final listViewFinder = find.byType(ListView);
      await tester.drag(listViewFinder, const Offset(0, -300));
      await tester.pumpAndSettle();
      
      final initialPosition = _getScrollPosition(tester);
      print('Initial scroll position after manual drag: $initialPosition');
      expect(initialPosition, equals(300.0));
      
      // 通过公共方法间接测试 _dragScrollbar（负delta = 向上滚动）
      print('Calling dragScrollbarUp...');
      await tester.dragScrollbarUp(30.0);
      await tester.pumpAndSettle(const Duration(seconds: 2));
      
      final newPosition = _getScrollPosition(tester);
      print('Final scroll position: $newPosition');
      
      // 验证向上滚动发生
      expect(newPosition, equals(initialPosition));
      print('=== Test completed successfully ===');
    });

    testWidgets('test _dragScrollbar with custom finder', (WidgetTester tester) async {
      print('=== Starting test: _dragScrollbar with custom finder ===');
      
      // 构建包含多个滚动条的测试组件
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: Row(
              children: [
                Expanded(
                  child: Scrollbar(
                    key: const Key('left-scrollbar'),
                    child: ListView.builder(
                      itemCount: 15,
                      itemBuilder: (context, index) => Container(
                        height: 80,
                        color: Colors.red[100],
                        child: Center(child: Text('Left $index')),
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Scrollbar(
                    key: const Key('right-scrollbar'),
                    child: ListView.builder(
                      itemCount: 15,
                      itemBuilder: (context, index) => Container(
                        height: 80,
                        color: Colors.blue[100],
                        child: Center(child: Text('Right $index')),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      );
      await tester.pumpAndSettle(const Duration(seconds: 2));
      
      // 针对特定滚动条操作
      final rightScrollbarFinder = find.byKey(const Key('right-scrollbar'));
      final initialPosition = _getScrollPosition(tester, rightScrollbarFinder);
      print('Initial position of right scrollbar: $initialPosition');
      
      print('Calling dragScrollbarDown with custom finder...');
      await tester.dragScrollbarDown(40.0, rightScrollbarFinder);
      await tester.pumpAndSettle(const Duration(seconds: 2));
      
      final newPosition = _getScrollPosition(tester, rightScrollbarFinder);
      print('Final position of right scrollbar: $newPosition');
      
      expect(newPosition, equals(0.0));
      print('=== Test completed successfully ===');
    });

    testWidgets('test _dragScrollbar edge cases', (WidgetTester tester) async {
      print('=== Starting test: _dragScrollbar edge cases ===');
      
      await tester.pumpWidget(_buildSimpleScrollable());
      await tester.pumpAndSettle(const Duration(seconds: 2));
      
      // 测试小幅度拖动
      print('Testing small delta...');
      await tester.dragScrollbarDown(10.0);
      await tester.pumpAndSettle();
      
      final position1 = _getScrollPosition(tester);
      print('Position after small drag: $position1');
      expect(position1, equals(0.0));
      
      // 测试大幅度拖动
      print('Testing large delta...');
      await tester.dragScrollbarDown(100.0);
      await tester.pumpAndSettle();
      
      final position2 = _getScrollPosition(tester);
      print('Position after large drag: $position2');
      expect(position2, equals(0.0));
      
      // 测试连续操作
      print('Testing consecutive operations...');
      await tester.dragScrollbarUp(60.0);
      await tester.pumpAndSettle();
      
      final position3 = _getScrollPosition(tester);
      print('Position after upward drag: $position3');
      expect(position3, equals(0.0));
      
      print('=== All edge case tests completed successfully ===');
    });
  });
}